/**
 * 
 */
package de.midnightdance.admin.bund;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.cbf.util.ui.editentities.interfaces.IEditableEntity;
import de.cbf.util.ui.editentities.template.AbstractCompositeEditEntity;

import de.midnightdance.admin.ws.entities.Bund;


/**
 * @author Stefan Alter
 *
 */
public class CompositeEditBund extends AbstractCompositeEditEntity {
	
	private Text txtName;
	private Text txtNotizen;
	//
	private ModifiedNameAdvancer 	modifiedName;
	private ModifiedNotizenAdvancer modifiedNotizen;

	/**
	 * @param parent
	 * @param style
	 */
	public CompositeEditBund(Composite parent, int style) {
		super(parent, style);		
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.template.AbstractCompositeEditEntity#setEnabled(boolean)
	 */
	@Override
	public void setEnabled(boolean enabled) {		
		txtName			.setEnabled(enabled);
		txtNotizen		.setEnabled(enabled);		
		super.setEnabled(enabled);
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.template.AbstractCompositeEditEntity#checkEntity(de.cbf.util.ui.editentities.interfaces.IEditableEntity)
	 */
	@Override
	protected boolean checkEntity(IEditableEntity value) {
		return (value instanceof Bund);
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.template.AbstractCompositeEditEntity#createCompositeEntity(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Composite createCompositeEntity(Composite parent) {
		//build listeners
		modifiedName 		= new ModifiedNameAdvancer();
		modifiedNotizen		= new ModifiedNotizenAdvancer();		
		//
		Composite main = new Composite(parent, SWT.NONE);
		//
		main.setLayout(new GridLayout(2, false));
		
		Label lblEditieren = new Label(main, SWT.NONE);
		lblEditieren.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		lblEditieren.setText("Bund editieren:");
		
		Label lblName = new Label(main, SWT.NONE);
		lblName.setText("Name:");
		
		txtName = new Text(main, SWT.BORDER);
		GridData gd_txtName = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_txtName.widthHint = 60;
		txtName.setLayoutData(gd_txtName);		
		
		Label lblNotizen = new Label(main, SWT.NONE);
		lblNotizen.setText("Notizen:");
		
		txtNotizen = new Text(main, SWT.BORDER);
		GridData gd_textPunkte = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
		gd_textPunkte.widthHint = 60;
		txtNotizen.setLayoutData(gd_textPunkte);				
		//
		initUI();
		registerListeners();
		//
		return main;
	}
	
	private void initUI() {
		txtName			.setText("");
		txtNotizen		.setText("");		
	}
	
	private void registerListeners() {	
		txtName			.addModifyListener		(modifiedName);
		txtNotizen		.addModifyListener		(modifiedNotizen);
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity#clear()
	 */
	@Override
	public void clear() {
		setBundName("");
		setNotizen("");
	}
	
	@Override
	protected void setEntityData(IEditableEntity value) {
		if (value instanceof Bund) {
			Bund entity = (Bund) value;
			setBund(entity);
		}		
	}	
	
	public Bund getBundData(Bund entity){
		entity.setName(getBundName());
		entity.setNotizen(getNotizen());		
		return entity;
	}
	
	public void setBund(Bund entity){
		setBundName		(entity.getName());
		setNotizen		(entity.getNotizen());		
	}
	
	public String getBundName(){
		return txtName.getText();
	}	
	public void setBundName(String name){
		Point selection = txtName.getSelection();
		txtName.setText(name);
		txtName.setSelection(selection);
	}
	
	public String getNotizen(){		
		return txtNotizen.getText();
	}	
	public void setNotizen(String value){
		Point selection = txtNotizen.getSelection();
		txtNotizen.setText(value);
		txtNotizen.setSelection(selection);
	}
	
	private Collection<ActionListener> modifiedNameListeners = new ArrayList<ActionListener>();
	public boolean registerModifiedNameListener(ActionListener listener){
		return modifiedNameListeners.add(listener);
	}
	public boolean deRegisterModifiedNameListener(ActionListener listener){
		return modifiedNameListeners.remove(listener);
	}
	
	private class ModifiedNameAdvancer implements ModifyListener{		
		@Override
		public void modifyText(ModifyEvent e) {
			ActionEvent event = new ActionEvent(CompositeEditBund.this, ActionEvent.ACTION_PERFORMED, "modifiedName");
			for (ActionListener listener : modifiedNameListeners) {
				listener.actionPerformed(event);
			}
		}		
	}
	private Collection<ActionListener> modifiedNotizenListeners = new ArrayList<ActionListener>();
	public boolean registerModfiedNotizenListener(ActionListener listener){
		return modifiedNotizenListeners.add(listener);
	}
	public boolean deRegisterModfiedNotizenListener(ActionListener listener){
		return modifiedNotizenListeners.remove(listener);
	}	
	private class ModifiedNotizenAdvancer implements ModifyListener{		
		@Override
		public void modifyText(ModifyEvent e) {
			ActionEvent event = new ActionEvent(CompositeEditBund.this, ActionEvent.ACTION_PERFORMED, "ModifiedNotizen");
			for (ActionListener listener : modifiedNotizenListeners) {
				listener.actionPerformed(event);
			}
		}		
	}

}
